#!/bin/bash
ecocycData="afer243159.profil"
working_directory="./big_instance/"

libs_dir="./libs"
java_cp="$libs_dir/jgrapht-jdk1.6.jar:$libs_dir/colt-1.2.0.jar:$libs_dir/collections-generic-4.01.jar"
#-------------------------------------------#
#                "Gint Creation"            #
#-------------------------------------------#
metabolite_cutoff="25"

echo "Gint Creation"
#Creating Gint called "modele.txt" in a $working_directory from ecocyc flat-file described in the profile $ecocycData. Metabolites that appreas in more than $metabolite_cutoff reactions are removed. The Gint metric is the colocalization distance.
java -classpath sipper.jar:"$java_cp" sipper.CreationGintFromEcocyc "$ecocycData" "$working_directory" "$metabolite_cutoff"

#Creating Gint called "modele.txt" in a $working_directory from ecocyc flat-file described in the profile $ecocycData. Metabolites that appreas in more than $metabolite_cutoff reactions are removed. The Gint metric is coded into a NxN matrice file with the following this format:
#					"gene_1_id	gene_2_id	...	gene_N_id"
#					"val_1_1	val_1_2	... val_1_N"
#					"val_2_1	val_2_2	... val_2_N"
#					"..."
#					"val_N_1	val_N_2	... val_N_N"
#
# The first line list the gene id of each gene
# val_i_j code for the metric from gene_i_id to gene_j_id.
# Each id or value is separate from other by a tabulation

#java -classpath sipper.jar:"$java_cp" sipper.CreationGintFromEcocyc "$ecocycData" "$working_directory" "$metabolite_cutoff" "$matrix"

#-------------------------------------------#
#           "k-SIPs Computation"            #
#-------------------------------------------#

GintFile="${working_directory}modele.txt"
k="10"
beginReactionsFile="${working_directory}associationsR.txt"
endReactionsFile="${working_directory}associationsR.txt"
THREADs="2"
kSIPsFile="${working_directory}result_${k}.txt"

echo "\n\n${k}-SIPs Computation"
#Computation of each possible k-SIP (written then into $kSIPsFile) that goes from each reaction in $beginReactionsFile to each reaction  in $endReactionsFile. Note that $beginReactionsFile and $endReactionsFile are file that contains list of named set of sources vertices. It is possible to use other sets of vertices until each set id is unique. The algorithme can be speedup by using many $THREADs (1 k-SIP computation use 1 thread).
java -classpath sipper.jar:"$java_cp" sipper.ComputeSIPs "$GintFile" "$beginReactionsFile" "$endReactionsFile" "$k" "$kSIPsFile" "$THREADs"


#-------------------------------------------#
#           "k-SIPs Translation"            #
#-------------------------------------------#

translatedKSIPsFile="${working_directory}result_10.translate"
GintFiles="${working_directory}modele.profil"
echo "\n\n${k}-SIPs Translation"
# translate k-SIPs in with integer form into a string form, easier to understand.
java -classpath sipper.jar:"$java_cp" sipper.Translate "$kSIPsFile" "$GintFiles" "$ecocycData" "$translatedKSIPsFile"

#-------------------------------------------#
#         "(sub)k-SIPs Extraction"          #
#-------------------------------------------#
# For historical reason, the translation must be used before the extraction, which slow down the workspeed and increase the memory usage. A workaround is envisaged


subsetType="bestSubset"
#subsetType="best2"
#subsetType="first5"
filteringMesure="dG"
#filteringMesure="wd"
#filteringMesure="i"
filteringValue="0.7"
# Extract some k-SIPs from $translatedKSIPsFile into the $working_directory. Many methods are possibles.
# $subsetType must take value in {bestSubset, bestZ, firstZ}, with Z a positive integer (e.g best5 or first7).
#  - bestSubset is the subset of paths in KSIP that optimize the $filteringMesure
#  - bestZ is the subset of Z paths in KSIP that optimize the $filteringMesure
#  - firstZ is the subset Z first path in KSIP according to the find order in sipper.ComputeSIPs
# $filteringMesure is optionnal. It must take value in {wd, dG, i}, where wd is the neighbouring coefficent, and dG is the genomic density and i the minimum gene sequence in the genome that contains all the gene of the k-SIP. When $filteringMesure is not set, the weight of the $k$-SIP is used as a mesure
# $filteringValue is also optionnal. It depends of $filteringMesure. When $filteringMesure="dG", only (sub)k-SIPs with dG>=$filteringValue are selected. When $filteringMesure is not set, or $filteringMesure="wd", or $filteringMesure="i" only (sub)k-SIPs with dG<=$filteringValue are selected.

echo "\n\n${k}-SIPs Extraction: ${subsetType} with ${filteringMesure} and filter at ${filteringValue}"
java -classpath sipper.jar:"$java_cp" sipper.Extract "$translatedKSIPsFile" "$ecocycData" "$working_directory" "$subsetType" "$filteringMesure" "$filteringValue"

#java -classpath sipper.jar:"$java_cp" sipper.Extract "$translatedKSIPsFile" "$ecocycData" "$working_directory" "$subsetType" "filteringMesure"

# It is possible to change recalculate the score of each $k$-SIP.
#java -classpath sipper.jar:"$java_cp" sipper.Extract "$translatedKSIPsFile" "$ecocycData" "$working_directory" "$subsetType"


#-------------------------------------------#
#         "k-SIPs modules Creation"         #
#-------------------------------------------#
#
#
# The dot export does not work with Java-SE 1.6.0_24. Must be investigated. Sorry!

modulesInput="${kSIPsFile%.*}_${subsetType}_${filteringMesure}_${filteringValue}.txt"

echo "\n\n(sub)${k}-SIPs Creation"
echo "The dot export does not work with Java-SE 1.6.0_24 but with Java machine from Eclipse. Must be investigated. Sorry!"
java -classpath sipper.jar:"$java_cp" sipper.ComputeKSIPsModules "$modulesInput" "${working_directory}"

modulesRep="${modulesInput%.*}_modules"

#.dot into .jpg
`sh ./convert.sh ${modulesRep}`
